package com.bt.lms.service.impl;

import com.bt.lms.constant.BOConstant;
import com.bt.lms.entity.Major;
import com.bt.lms.entity.page.Page;
import com.bt.lms.dao.servise.MajorDao;
import com.bt.lms.dao.servise.impl.MajorDaoImpl;
import com.bt.lms.service.MajorService;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 专业服务
 *
 * @author 徐大永
 */
public class MajorServiceImpl implements MajorService {

    @Override
    public Page<Major> pagingQuery(Page<Major> page) {

        //存放专业的对象
        List<Major> majors = new ArrayList<>();
        page.setRecords(majors);

        //dao对象
        MajorDao majorDao = new MajorDaoImpl();

        //查询专业信息 编码，名称，状态
        StringBuilder majorSqlSB = new StringBuilder();
        majorSqlSB.append("SELECT * FROM major WHERE deleted=").append(BOConstant.DELETED_FALSE).append(" LIMIT ?,?");
        String majorSql = new String(majorSqlSB);
        //查询总数
        StringBuilder majorSumSqlSB = new StringBuilder();
        majorSumSqlSB.append("SELECT COUNT(*) FROM major WHERE deleted=").append(BOConstant.DELETED_FALSE);
        String majorSumSql = new String(majorSumSqlSB);
        //dao方法 得全部用户基本信息（在集合中）
        majorDao.query(majorSql, page, majors, majorSumSql);

        return page;
    }

    @Override
    public Major detail(Long id) {

        //sql
        String sql = "SELECT * FROM major WHERE id = ?";

        //dao 方法
        MajorDao majorDao = new MajorDaoImpl();

        return majorDao.select(sql, id);
    }

    @Override
    public boolean add(Major major) {

        //和几张表有关系 major表
        //1.系统添加的属性
        UUID uuid = UUID.randomUUID();
        String string = uuid.toString();
        major.setCode(string);
        major.setStatus(BOConstant.MAJOR_INFO_STATUS_STOP);

        //sql
        String sql = "INSERT INTO major (name,code,status,details) VALUES (?,?,?,?)";

        //dao 方法
        MajorDao majorDao = new MajorDaoImpl();
        majorDao.insert(sql, major);

        return true;
    }

    @Override
    public boolean compile(Major major) {

        //sql
        String sql = "UPDATE major SET name=?,details=? WHERE id=?";

        //dao 调用
        MajorDao majorDao = new MajorDaoImpl();
        int i = majorDao.update(sql, major);

        return i == 1;
    }

    @Override
    public boolean del(Long id) {

        //sql
        StringBuilder sqlSB = new StringBuilder();
        sqlSB.append("UPDATE major SET deleted=").append(BOConstant.DELETED_TRUE).append(" WHERE id=?");
        String sql = new String(sqlSB);

        //dao方法
        MajorDao majorDao = new MajorDaoImpl();
        int i = majorDao.update(sql, id);

        return i == 1;
    }

    @Override
    public boolean studyCenSetMajor(Long stuCenId, List<Long> majorIds) {

        //要执行的sql
        String sql = "INSERT rl_major_study_centre (major_id,study_centre_id) VALUES (?,?)";

        //dao对象 方法
        MajorDao majorDao = new MajorDaoImpl();
        int i = majorDao.insertRls(sql, majorIds, stuCenId);

        return i == majorIds.size();
    }

    @Override
    public boolean start(Long majorId) {

        //sql
        StringBuilder sqlSB = new StringBuilder();
        sqlSB.append("UPDATE major SET status=").append(BOConstant.MAJOR_INFO_STATUS_START).append(" WHERE id=?");
        String sql = new String(sqlSB);

        //dao方法
        MajorDao majorDao = new MajorDaoImpl();
        int update = majorDao.update(sql, majorId);

        return update == 1;

    }

    @Override
    public boolean stop(Long majorId) {

        //sql
        StringBuilder sqlSB = new StringBuilder();
        sqlSB.append("UPDATE major SET status=").append(BOConstant.MAJOR_INFO_STATUS_STOP).append(" WHERE id=?");
        String sql = new String(sqlSB);

        //dao方法
        MajorDao majorDao = new MajorDaoImpl();
        int update = majorDao.update(sql, majorId);

        return update == 1;
    }
}
